<?php

namespace STAGE\StageBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use STAGE\StageBundle\Entity\TypeStage;
use STAGE\StageBundle\Form\TypeStageType;

class TypeStageController extends Controller {

    public function indexAction() {
        return $this->render('StageBundle:TypeStage:index.html.twig');
    }

    public function newAction() {
        $request = $this->getRequest();
        $em = $this->getDoctrine()->getManager();
        $id = $request->get('id');
        if ($id == '')
            $type = new TypeStage();
        else
            $type = $em->getRepository('StageBundle:TypeStage')->find($id);
        $form = $this->createForm(new TypeStageType(), $type);
        if ($request->isMethod('post')) {
            $form->bind($request);
//            if($form->isValid()){
            $type = $form->getData();
            $em->persist($type);
            $em->flush();
            $url = $this->generateUrl('stage_type_stage_list');
            return $this->redirect($url);
//            }
        }
        return $this->render('StageBundle:TypeStage:new.html.twig', array('id' => $id, 'form' => $form->createView()));
    }

    public function listAction() {
        $em = $this->getDoctrine()->getManager();
        $types = $em->getRepository('StageBundle:TypeStage')->findAll();
        return $this->render('StageBundle:TypeStage:list.html.twig', array('types' => $types));
    }

    public function deleteAction() {
        $request = $this->getRequest();
        $em = $this->getDoctrine()->getManager();
        $id = $request->get('id');
        $type = $em->getRepository('StageBundle:TypeStage')->find($id);
        $em->remove($type);
        $em->flush();
        $url = $this->generateUrl('stage_type_stage_list');
        return $this->redirect($url);
    }

}
